const express = require("express");
const router = express.Router();
const { conMysql } = require("../db");

//添加业务员 字段：uid  uname uphone uphoto enterprises position  uID GX00001开头 自增
router.post("/addSalesperson", (req, res) => {

    let { uname, uphone, uphoto, enterprises, position } = req.body;
    //判断是否为空
    if (!uname || !uphone || !uphoto || !enterprises || !position) {
        res.output("添加失败", 500, null);
        return;
    }
    // 查找最后一条数据的uid
    const sql = `select uid from db_salesperson order by uid desc limit 1`;

    conMysql(sql)
        .then((result) => {
            if (result.length == 0) {
                result = [{ uid: 'GX00000' }];
            }
            console.log(result);

            let uid = "GX" + (parseInt(result[0].uid.slice(2)) + 1).toString().padStart(5, "0");

            const sql = `insert into db_salesperson (uid,uname,uphone,uphoto,enterprises,position) values ('${uid}','${uname}','${uphone}','${uphoto}','${enterprises}','${position}')`;
            conMysql(sql)
                .then((result) => {
                    res.output("添加成功", 0, null);
                })
                .catch((err) => {

                    res.output("添加失败", 500, null);
                });
        })
        .catch((err) => {
            console.log(err);
            res.output(err, 500, null);
        });
})

module.exports = router;